var lunboImgesUrl = "";
var imageUrlList = [[],[],[],[],[],[],[],[],[],[],[],[]];
var countdown=60;
var phone="";
var phoneStatus = false;

function saveApply(){
    if($("#applyName").val() == null ||　$("#applyName").val() == ""){
        alert("票据名称不可为空");
        return;
    }
    if($("#applyCompanyName").val() == null ||　$("#applyCompanyName").val() == ""){
        alert("单位名称不可为空");
        return;
    }
    if($("#applyType").val() == 0){
        alert("请选择审查类型");
        return;
    }
    if($("#applyPerson").val() == null ||　$("#applyPerson").val() == ""){
        alert("单位联系人不可为空");
        return;
    }
    if($("#applyAddr").val() == null ||　$("#applyAddr").val() == ""){
        alert("申请单位地址不可为空");
        return;
    }
    if($("#applyPhone").val() == null ||　$("#applyPhone").val() == ""){
        alert("申请单位电话不可为空");
        return;
    }else{
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if (!myreg.test($("#applyPhone").val())) {
            alert("请输入正确的手机号码。");
            return;
        }
    }
    if(!phoneStatus){
        verify();
        alert("手机号码验证未通过");
        return;
    }
    if($("#applyStreet").val() == null ||　$("#applyStreet").val() == "" || $("#applyStreet").val() == 0){
        alert("申请单位街道不可为空");
        return;
    }
    for(var i=0; i<imageUrlList.length; i++){
        if(imageUrlList[i].length == 0){
            var imgTypeName = getTypeName(i+1);
            alert("类型 : " + imgTypeName + "，图片不可空");
            return;
        }
    }

    var applyDetailList = getDetailList(imageUrlList);

    var data = {
       /* applyNo : $("#applyNo").val(),*/
        applyName : $("#applyName").val(),
        applyCompanyName : $("#applyCompanyName").val(),
        applyType : $("#applyType").val(),
        applyPerson : $("#applyPerson").val(),
        applyAddr : $("#applyAddr").val(),
        applyPhone : $("#applyPhone").val(),
        applyStreet : $("#applyStreet").val(),
        imgUrl : lunboImgesUrl,
        detailList : applyDetailList
    };
    $.ajax({
        url :   prefixUrl + "apply/addapply",
        type:   "post",
        data:JSON.stringify(data),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            alert(data.msg);
            //queryApplyList();
            location.href="apply.html";
        }
    });
}


function choiceImg(type) {
    window.imageUrlList = imageUrlList;
    window.addStatus = false;
    //window.open ('choiceImg.html?type=' + type, 'newwindow', 'height=800, width=1200, top=300, left=550, toolbar=no, menubar=no, resizable=no,location=no, status=no,titlebar=no,');
    window.open('choiceImg.html?type=' + type);
}

function getImgUrl(imgUrls,type) {
    if(imgUrls.length > 0 && imgUrls.length != 1){
        imgUrls = imgUrls.substring(0,imgUrls.length - 1);
        var imgList = imgUrls.split(",");
        var list = [];
        for(var i=0; i<imgList.length; i++){
            console.log("#img" + type);
            if(i == 0){
                $("#img" + type).attr("src","img/type-" + type + ".png");
            }
            var imgData = {
                "imgUrl" : imgList[i],
                "imgType": type
            };
            list[i] = imgData;
        }
        imageUrlList[type-1] = list;
    }else{
        imageUrlList[type-1] = [];
        $("#img" + type).attr("src","img/type" + type + ".png");
    }
}

function getTypeName(type) {
    switch(type){
        case 1 : return "情况说明";break;
        case 2 : return "安全保证书";break;
        case 3 : return "街路平面位置图";break;
        case 4 : return "景观实景图";break;
        case 5 : return "景观效果图";break;
        case 6 : return "房屋证明材料";break;
        case 7 : return "协议书";break;
        case 8 : return "工商营业执照";break;
        case 9 : return "法人身份证";break;
        case 10 : return "安全鉴定书";break;
        case 11 : return "施工设计图";break;
        case 12 : return "消防证明";break;
    }
}

function getDetailList(imageUrlList){
    var list = [];
    var number = 0;
    for(var i=0; i<imageUrlList.length; i++){
        var typeList = imageUrlList[i];
        for(var j=0; j<typeList.length; j++){
            var data = typeList[j];
            list[number] = data;
            number++;
        }
    }
    return list;
}

function getPhoneCode(){
    phone = $("#applyPhone").val();
    if($("#applyPhone").val() == null || $("#applyPhone").val() == ""){
        alert("请输入手机手机号码");
        return;
    }
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test($("#applyPhone").val())) {
        alert("请输入正确的手机号码。");
        return;
    }
    var data = {
        phone : $("#applyPhone").val(),
    }
    $.ajax({
        url :   prefixUrl + "apply/sendMessage",
        type:   "post",
        contentType:"application/json",
        data:JSON.stringify(data),
        success:function(data){
            goToLogin(data);
            if(data.code == 0){
                settime();
            }
            alert(data.msg);
        }
    })
}

function settime() {
    if (countdown == 0) {
        $("#btn_code").attr("disabled",false);
        $("#btn_code").val("获取验证码");
        countdown = 60;
        return false;
    } else {
        $("#btn_code").attr("disabled", true);
        $("#btn_code").val("重新发送(" + countdown + ")");
        countdown--;
    }
    setTimeout(function() {
        settime();
    },1000);
}

$("#applyPhone").change(function (e) {
    if($("#applyPhone").val() == null || $("#applyPhone").val() == ""){
        alert("请输入联系电话");
        return;
    }
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test($("#applyPhone").val())) {
        alert("请输入正确的手机号码。");
        return;
    }
})


$("#phoneCode").change(function (e) {
    verify();
})

function verify(){
    if($("#phoneCode").val() == null || $("#phoneCode").val() == ""){
        return;
    }
    if($("#applyPhone").val() == null || $("#applyPhone").val() == ""){
        alert("请先获取验证码");
        return;
    }
    if($("#applyPhone").val() != phone && phone != ""  && phone != undefined){
        alert("手机号码已变化，请重新操作");
        return;
    }
    var data = {
        phone : $("#applyPhone").val(),
        code : $("#phoneCode").val()
    }
    $.ajax({
        url :   prefixUrl + "apply/checkMessage",
        type:   "post",
        contentType:"application/json",
        data:JSON.stringify(data),
        success:function(data){
            goToLogin(data);
            if(data.code == 0){
                phoneStatus = true;
                $("#tips").val("校验通过");
            }else{
                $("#tips").val("验证码错误");
            }
        }
    })
}
